#include <basic_types.h>
#include <mediatypes.h>
#include <osal/osal.h>
#include <api/libc/printf.h>
#include <hld/osd/osddrv.h>
#include <api/libttx/ttx_osd.h>
#include <api/libttx/lib_ttx.h>
#include <hld/hld_dev.h>
#include <hld/dis/vpo.h>

extern volatile BOOL ttx_show_on;
extern VSCR TTX_Vscr ;
extern struct osd_device *osd_dev;
extern UINT8 m_bCurRegion;
extern struct ttx_config_par g_ttx_pconfig_par;

extern UINT32 osd_ttx_width;
extern UINT32 osd_ttx_height;
//extern UINT32 osd_ttx_startcol;
//extern UINT32 osd_ttx_startrow;
extern UINT32 osd_ttx_xoffset;
extern UINT32 osd_ttx_yoffset;

extern UINT32* find_char(UINT16 charset, UINT8 character, UINT8 p26_char_set);

const UINT8 ttx_pallette_256[4*256] = {
				/*front*/
				0x10, 0x80, 0x80, 0x0f,	//black
				0x50, 0x5b, 0xec, 0x0f,	//red
				0x8f, 0x37, 0x24, 0x0f,	//green
				0xd0, 0x12, 0x90, 0x0f,	//yellow
				0x27, 0xec, 0x6e, 0x0f,	//blue
				0x68, 0xc7, 0xda, 0x0f,	//magenta
				0xa7, 0xa3, 0x12, 0x0f,	//cyan
				0xea, 0x7f, 0x7f, 0x0f,	//white
				/*back*/
				0x10, 0x80, 0x80, 0x0f,	//black   
				0x50, 0x5b, 0xec, 0x0f,	//red
				0x8f, 0x37, 0x24, 0x0f,	//green
				0xd0, 0x12, 0x90, 0x0f,	//yellow
				0x27, 0xec, 0x6e, 0x0f,	//blue
				0x68, 0xc7, 0xda, 0x0f,	//magenta
				0xa7, 0xa3, 0x12, 0x0f,	//cyan
				0xea, 0x7f, 0x7f, 0x0f,	//white

				0, 0, 0, 0,	//transparent

                0x10, 0x80, 0x80, 0x0f,	//black-instead
                0x50, 0x5b, 0xec, 0x0f,	//red-instead
				0x8f, 0x37, 0x24, 0x0f,	//green-instead
				0xd0, 0x12, 0x90, 0x0f,	//yellow-instead
				0x27, 0xec, 0x6e, 0x0f,	//blue-instead
				0x68, 0xc7, 0xda, 0x0f,	//magenta-instead
				0xa7, 0xa3, 0x12, 0x0f,	//cyan-instead
				0xea, 0x7f, 0x7f, 0x0f,	//white-instead
                
};

void TTX_DrawChar_256(UINT16 x, UINT16 y, UINT16 charset, UINT8 fg_color, UINT8 bg_color,UINT8 double_width,UINT8 double_height, UINT8 character, UINT8 p26_char_set)
{
	UINT8* pData; 
	struct OSDRect r;
      INT16 dw,dh;
	UINT8 charset_idx, char_idx;
	UINT32 charmask;
	UINT16 bit_count;
	UINT8 i, j, k;
	UINT8 data1, data2;
	UINT8 zoomkind;
	UINT8 temp;
	UINT32*  char_addr;


	if(ttx_show_on!=TRUE)
		return;
	
	//if(character < 0x20 ||character > 0x7f)
	//	return;
	if((x%TTX_CHAR_W) != 0 || (y%TTX_CHAR_H) != 0 || x+TTX_CHAR_W > osd_ttx_width)
	{
		//libc_printf("-----------------TTX_DrawChar(), parameter illegal,x=%d,y=%d\n",x,y);
		return ;
	}

    if(get_ttxmenu_status()== TRUE && get_ttxinfo_status() == TRUE)
    {
        if(y+TTX_CHAR_H > osd_ttx_height+TTX_CHAR_H)
            return;
    }
    else
    {
        if(y+TTX_CHAR_H > osd_ttx_height)
            return;
    }

	if(double_width && (x>=39*TTX_CHAR_W))
		double_width = 0;
	if(double_height && (y>=24*TTX_CHAR_H))
		double_height = 0;
	
	 if(double_width)  
	 	{
			
			//PRINTF("---------TTX_DrawChar(), OSD_STRIDE = %d \n", OSD_STRIDE);
			dw = 28;
			if(double_height) //vertical zoom double
				{
					dh = 40;
					zoomkind = 4;
				}
			else		// vertical normal
				{
					dh = 20;
					zoomkind = 3;
				}
	 	}
	 else	//horizion normal	
	 	{
			
			//PRINTF("---------TTX_DrawChar(), OSD_STRIDE = %d \n", OSD_STRIDE);
			dw = 14;
			if(double_height)	//vertical zoom  double
				{
					dh = 40;
					zoomkind = 2;
				}
			else		//vertical normal
				{
					dh = 20;
					zoomkind = 1;
				}
	 	}
	//TTX_PRINTF("---------TTX_DrawChar(), -----2, double_width=%d, double_height=%d\n",double_width, double_height);
		
	//set TTX_Vscr size
	
	r.uLeft = x + osd_ttx_xoffset;
	r.uTop = y + osd_ttx_yoffset;
	r.uWidth = dw;
	r.uHeight = dh;
		
	TTX_Vscr.vR.uLeft = r.uLeft;
	TTX_Vscr.vR.uTop = r.uTop;
	TTX_Vscr.vR.uWidth = TTX_CHAR_W*2;
	TTX_Vscr.vR.uHeight = TTX_CHAR_H*2;
	
	TTX_Vscr.lpbScr = g_ttx_pconfig_par.ttx_vscrbuf;
	pData = TTX_Vscr.lpbScr;
			

	bit_count = 0;
	char_addr = find_char( charset, character, p26_char_set);
	if(char_addr == NULL) return;
	
				switch(zoomkind)
				{
				     case 1:			//vertical normal , horizine normal
				     	for(i = 0 ;i < 9;i++)
				     	{
				     		charmask = 0x80000000;	
						for(j = 0 ;j < 32 ;j++)
		   				{
							if(bit_count >= TTX_CHAR_W * TTX_CHAR_H)	//char draw finished
								break;
						
							if(bit_count!=0 && bit_count %14 ==0)	//one row finished, change to next
								pData += 14;
							//TTX_PRINTF("------------------charmask = %x \n",charmask);
							//16 color, 2 pixels per byte 
							//if(TTX_Font[charset_idx][char_idx][i] & charmask)
							if(*char_addr & charmask)
								data1 = fg_color;
							else
								data1 = bg_color;
					
							*pData = data1;
							//TTX_PRINTF("-----TTX_DrawChar(),-----4,-----normal, pData = %d, *pData = %x\n",  pData, *pData);

							pData++;
							bit_count ++;
							charmask = charmask>>1;
															
		   				}
						char_addr += 1;
						//TTX_PRINTF("-----TTX_DrawChar(),-----4,   -----normal,  %d  UINT32 = %x draw finished!\n", i, TTX_Font[charset_idx][char_idx][i]);
				     	}
						break;
					case 2:			//vertical zoom  double, horizine normal
					 for(i = 0 ;i < 9;i++)
				     	 {
				     		charmask = 0x80000000;	
					      for(j = 0;j < 32;j++)
						{	
							if(bit_count >= TTX_CHAR_W * TTX_CHAR_H)
								break;
							if(bit_count!=0 && bit_count %14 ==0)
       							pData += 14*3;
       						//if(TTX_Font[charset_idx][char_idx][i] & charmask)
       						if(*char_addr & charmask)
								data1 = fg_color;
							else
								data1 = bg_color;

							*pData = data1;
							//TTX_PRINTF("-----TTX_DrawChar(),-----4,-----double uHeight, pData = %d, *pData = %x\n",  pData, *pData);
							*(pData + 14*2) = *pData;
							//TTX_PRINTF("-----TTX_DrawChar(),-----4,-----double uHeight, pData+14 = %d, *(pData+14) = %x\n",  pData+14, *(pData + 14));
       						pData++;
       						bit_count ++;
       						charmask = charmask>>1;
						}
						//TTX_PRINTF("-----TTX_DrawChar(),-----4,-----double uHeight,  %d UINT32 = %x draw finished!\n", i, TTX_Font[charset_idx][char_idx][i]);
						char_addr += 1;
					 }
						break;
					case 3:		 //horizine zoom  double, vertical normal
					   for(i = 0 ;i < 9;i++)
				     	  {
				     		charmask = 0x80000000;
					       for(j=0;j<32;j++)
						{
							if(bit_count >= TTX_CHAR_W * TTX_CHAR_H)
								break;
							//if(TTX_Font[charset_idx][char_idx][i] & charmask)
							if(*char_addr & charmask)
								data1 = fg_color;
							else
								data1 = bg_color;
						
							*pData = data1;
             						pData++;
							*pData = data1;							
							pData++;
							bit_count ++;
             						charmask = charmask>>1;
						}
						//TTX_PRINTF("-----TTX_DrawChar(),-----4,-----double uWidth,  %d UINT32 = %x draw finished!\n", i, TTX_Font[charset_idx][char_idx][i]);
						char_addr += 1;
					   }
						break;
					 case 4:			 //horizine zoom  double, vertical double
						for(i = 0 ;i < 9;i++)
				     		{
				     			charmask = 0x80000000;
							for(j = 0;j < 32;j++)
             						{
								if(bit_count == TTX_CHAR_W * TTX_CHAR_H)
									break;
								if(bit_count!=0 && bit_count % 14 ==0)
									pData += 14*2;
								//if(TTX_Font[charset_idx][char_idx][i] & charmask)
								if(*char_addr & charmask)
									data1 = fg_color;
								else
									data1 = bg_color;
					
								*pData = data1;
								*(pData + 14*2) = data1;
								*(pData + 14*2+1) = data1;
         							pData++;
								*pData = data1;
								//TTX_PRINTF("-----TTX_DrawChar(),-----4,-----double both, *pData = %x\n", *pData);
                      					pData++;
                      					charmask = charmask>>1;
								bit_count += 1;
             						}
             						//TTX_PRINTF("-----TTX_DrawChar(),-----4,-----double both,  %d UINT32 = %x  draw finished!\n", i, TTX_Font[charset_idx][char_idx][i]);
							char_addr += 1;
						}
							break;

					default:
							break;
				}
		
	// write Vscr data to osd frame buffer
	OSDDrv_RegionWrite((HANDLE)osd_dev, m_bCurRegion, &TTX_Vscr, &r);
}
